package org.tio.sitexxx.im.server.handler;

import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.MsgModel.KFP2PNty;
import org.tio.sitexxx.im.common.MsgModel.KFP2PReq;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.model.main.ChatRecord;
import org.tio.sitexxx.service.model.main.ChatRecordP2p;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.UserRoleService;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.stat.ChatRecordP2PService;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.json.Json;


@CommandHandler(Command.KF_PICTURE_REQ0)
public class KFP2PPictureHandler_ implements ImServerHandler {
    private static Logger log = LoggerFactory.getLogger(P2PChatReqHandler.class);

    public static final P2PChatReqHandler me = new P2PChatReqHandler();
    @Override
    public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
        SimpleUser fromUser = ImUtils.getSimpleUser(channelContext);
        if (fromUser == null) {
            ImUtils.pleaseLogin(channelContext, "登录才能聊天哦");
            return;
        }

        KFP2PReq req = Json.toBean(packet.getBodyStr(), KFP2PReq.class);
        System.out.println("req:"+req.getContent());
        String toUserid = req.getToId(); //发给谁

        String c = req.getContent(); //聊天内容
        if (StrUtil.isBlank(c)) {
            return;
        }

        String displayContent = c;//SensitiveWordsService.findAndReplace(c); //过滤敏感词后的聊天内容
        displayContent = StringEscapeUtils.escapeHtml4(displayContent);
        User toUser = UserService.ME.getByLoginname(toUserid+"",null);
        if (toUser == null) {
            Tio.remove(channelContext, "提供了错误的userid[" + toUserid + "]");
            return;
        } else {
            if (!UserRoleService.checkUserStatus(toUser)) {
                Tio.remove(channelContext, "提供了错误的userid[" + toUserid + "]");
                return;
            }
        }

        SimpleUser to = SimpleUser.fromUser(toUser);

        Byte contenttype = ChatRecord.ContentType.CARD_IMG_;
        p2pChatAndLog(channelContext, c, displayContent, fromUser, to, contenttype);
    }
    /**
     * 私聊，并保存日志
     * @param channelContext
     * @param initContent
     * @param displayContent
     * @param fromUser
     * @param to
     * @param contenttype
     */
    public static void p2pChatAndLog(ChannelContext channelContext, String initContent, String displayContent, SimpleUser fromUser, SimpleUser to, Byte contenttype) {
        String sessionid = ImUtils.getToken(channelContext);
        String fromIp = channelContext.getClientNode().getIp();
        ChatRecordP2p msg = ChatRecordP2PService.me.saveChatRecord(sessionid, initContent, displayContent, fromUser.getI(), fromIp, to.getI(), contenttype);
        KFP2PNty nty = new KFP2PNty(msg.getUid(),msg.getTouid(),msg.getId(),msg.getText(),msg.getTime().getTime(), fromUser);
        ImPacket imPacket = new ImPacket(Command.KF_PICTURE_NTY0,nty);
//        System.out.println("send picture impacket:"+ JSON.toJSONString(imPacket));
        Ims.sendToUser(to.getI(),imPacket);
    }
}
